import { getUrlQuery } from "@/utils/format";
import { getQrcodeScanDetail } from "@/service/api/sp-table-code/scan";
import { bindDesk } from "@/service/api/sp-product";

export default {
  methods: {
    async handleTableCodeManagementScan(record) {
      // const qrcode = getUrlQuery(
      //   "https://cloudtest.xiangsaopay.com/qr?type=order&deptId=1123598813738675201&code=FHB1230000000011"
      // );
      const qrcode = getUrlQuery(record?.detail?.result);
      const deviceNo = qrcode?.code;
      if (!deviceNo) return;
      const res = await getQrcodeScanDetail({ deviceNo });
      // console.log("扫一扫结果", record, res);
      this.isBind = res?.isBind;
      if (res?.isBind) {
        uni.showToast({
          title: "此桌码已绑定",
          icon: "none",
        });
        return;
      }
      await this.handleTableCodeManagementBindCode(deviceNo);
    },
    async handleTableCodeManagementBindCode(deviceNo) {
      // type=tableCodeManagement&id=34&areaId=11&tableNo=89&codeNo=&pubDomainBusinessId=1459014961741774849&pubDomainShopId=1459007033722880001
      let {
        pubUserInfo: {
          pubDomainShopVo: { storeNo },
          mobilePhone,
        },
      } = this;
      const { pubDomainShopId, id, areaId } = this.$Route.query;
      let params = {
        pubDomainShopId,
        qrCode: deviceNo,
        tableId: id,
        storeNo,
        name: areaId,
        phone: mobilePhone,
      };
      const res = await bindDesk(params).catch((err) => {
        uni.showToast({
          title: err.msg,
          icon: "none",
        });
      });
      if (res?.code === 0) {
        uni.showToast({
          title: "绑定成功",
          icon: "success",
        });
        setTimeout(() => {
          uni.navigateBack();
        }, 1500);
      }
    },
  },
};
